/**
 * <p>功能:系统管理模块-资源管理-菜单资源 <p>
 * @extends Ext.tab.Panel #e8edf0
 * @author wys
 */
Ext.define('Jason.view.protal.module.systemManager.sources.menus.MainCtrl', {
    extend: 'Wys.basic.BaseCtrl',
    alias: 'controller.sources_menus_MainViewCtrl',
    /** private*/
    formItems: function () {
        var treeComboStore = this.getView().getViewModel().getData().formParentAncChildTree;
        return [{
            xtype: 'fieldset',
            title: '基本信息',
            collapsible: false,
            defaultType: 'textfield',
            layout: {
                type: 'table',
                columns: 1
            },
            defaults: {
                width: '100%',
                labelWidth: 80
            },
            items: [{xtype:'hiddenfield',name:'id'},
                {
                    xtype: 'radiogroup', fieldLabel: '类型', graw: true,
                    listeners: {
                        render: function (self) {
                            var newValue = self.getValue();
                            if (newValue.extendclass === "modules") {
                                self.up().down('textfield[name=extendtype]').hide();
                                self.up().down('textfield[name=extendtype]').allowBlank = true;
                                self.up().down('textfield[name=view]').allowBlank = true;
                                self.up().down('textfield[name=view]').hide();
                                self.up().down('#haveRight').hide();
                            } else {
                                self.up().down('textfield[name=extendtype]').show();
                                self.up().down('textfield[name=extendtype]').allowBlank = false;
                                self.up().down('textfield[name=view]').allowBlank = false;
                                self.up().down('textfield[name=view]').show();
                                self.up().down('#haveRight').show();
                            }
                        },
                        change: function (self, newValue, oldValue) {
                            if (newValue.extendclass === "modules") {
                                self.up().down('textfield[name=extendtype]').hide();
                                self.up().down('textfield[name=extendtype]').allowBlank = true;
                                self.up().down('textfield[name=view]').allowBlank = true;
                                self.up().down('textfield[name=view]').hide();
                                self.up().down('#haveRight').hide();
                            } else {
                                self.up().down('textfield[name=extendtype]').show();
                                self.up().down('textfield[name=extendtype]').allowBlank = false;
                                self.up().down('textfield[name=view]').allowBlank = false;
                                self.up().down('textfield[name=view]').show();
                                self.up().down('#haveRight').show();
                            }
                        }
                    },
                    items: [
                        { boxLabel: '模块', name: 'extendclass', inputValue: 'modules', checked: true },
                        { boxLabel: '菜单', name: 'extendclass', inputValue: 'menu' }
                    ]
                },
                {
                    name: 'parent_', xtype: 'comboTree', fieldLabel: '父级', store: treeComboStore,
                    valueField: 'modularcode',
                    displayField: 'text',
                    autoScroll: true,
                    allowBlank: true
                },
                {
                    name: 'extendtype', xtype: 'textfield', fieldLabel: '路由', allowBlank: false, afterLabelTextTpl: cfg.reqired,
                    emptyText: '名称任意,不可重复,仅做层级显示'
                },
                {
                    name: 'view', xtype: 'textfield', fieldLabel: '页面视图', allowBlank: false, afterLabelTextTpl: cfg.reqired,
                    emptyText: '路由指向的页面视图'
                },
                {
                    xtype: 'fieldset',
                    itemId: 'haveRight',
                    columnWidth: 0.5,
                    layout: { type: 'table', columns: 9 },
                    title: '选择拥有的操作',
                    defaultType: 'checkboxfield',
                    listeners: {
                        afterrender: function (this_, eOpts) {
                            var ajaxCfg = {
                                url: reqUrl.sys.comboData.dict + "ModularRight",
                                callback: function (resp) {
                                    var formRecord = this_.up('form').dataModel;
                                    var items_field = new Array();
                                    Ext.Array.each(resp.root, function (items) {
                                        var item = {};
                                        item.boxLabel = items.key;
                                        item.inputValue = items.keycode;
                                        item.name = "modularright";
                                        item.margin = '0 5 0 0';
                                        item.checked = false;
                                        if (formRecord != undefined) {
                                            formDateRight = formRecord.data.modularright;
                                            item.checked =  formDateRight.includes(items.keycode);
                                        }
                                        items_field.push(item);
                                    });
                                    this_.add(items_field);
                                    single: true;
                                }
                            };
                            Ext.defer(function(){EUtil.RS(ajaxCfg);},500);
                        }
                    }
                },
                {
                    name: 'name', xtype: 'textfield', fieldLabel: '标题', allowBlank: false, afterLabelTextTpl: cfg.reqired,
                    emptyText: '请输入资源标题'
                },
                {
                    name: 'icon', xtype: 'iconField', fieldLabel: '图标', emptyText: 'fa fa-circle-o', allowBlank: false, afterLabelTextTpl: cfg.reqired

                },
                { name: 'order', xtype: 'numberfield', fieldLabel: '权重', value: 1, allowBlank: false, afterLabelTextTpl: cfg.reqired },
                { name: 'remarks', xtype: 'textarea', fieldLabel: '备注', graw: true, maxLength: 300, emptyText: '(选填)' }
            ]
        }];
    },
    onNewButtonClick: function (btn) {
        var me = this;
        Ext.create('Wys.form.FormWindow', {
            width: 524,
            title: '菜单资源管理',
            config: {
                operatetype: 'new',
                model: 'Jason.model.protal.module.systemManager.sources.menus.MenuModel',
                gridPanel: me.getView(),
                items: me.formItems()
            }
        }).show();
    },
    onEditButtonClick: function (grid, record) {
        var me = this;
        if (grid.xtype.indexOf('button') != -1) {
            records = grid.up('baseView').getSelection();
            if (records.length === 0 || records.length > 1) {
                EUtil.showWarning('请选择要编辑的数据。<br /><span style="color:red">注：一次只能选择一条数据进行编辑</span>');
                return;
            } else {
                record = records[0];
            }
        }
        Ext.create('Wys.form.FormWindow', {
            width: 524,
            title: ' 『' + record.get('name') + '』菜单资源信息',
            config: {
                model: 'Jason.model.protal.module.systemManager.sources.menus.MenuModel',
                operatetype: 'edit',
                gridPanel: grid,
                items: me.formItems(),
                formData: record
            }
        }).show();
    },
    onDeleteButtonClick: function (btn) {
        var grid = this.getView(),
            gridStore = grid.getStore(),
            selectGridData = grid.getSelection(),
            removePK = [],
            removeName = [];
        if (selectGridData.length === 0) {
            EUtil.toastWarn('请选择要操作的数据');
            return;
        }
        Ext.Array.each(selectGridData, function (item) {
            removeName.push(item.data.text);
            removePK.push(item.data.id);
        });
        EUtil.showMsg({
            option: 3,
            title: '温馨提示',
            message: '您确定删除<span style="color:red;">『' + removeName.join() + '』</span>吗？',
            callback: function (btn) {
                if (btn === 'ok') {
                    gridStore.remove(selectGridData);
                    EUtil.RS({
                        url: reqUrl.sys.sources.menus.destroy,
                        params: { ids: removePK },
                        callback: function (resp) {
                            if (resp.success) {
                                store.reload();
                            } else {
                                EUtil.showError(resp.msg || '删除资源『' + removeName.join() + '』失败！');
                            }
                        }
                    });
                }
            }
        });
    },
    onGridCommand: function (item, command, record, recordIndex, cellIndex) {
        var me = this,
            grid = this.getView(),
            store = grid.getStore();
        if (command === 'modify') {/** 修改 */
            Ext.create('Wys.form.FormWindow', {
                width: 524,
                title: '菜单资源',
                config: {
                    operatetype: 'edit',
                    model: 'Jason.model.protal.module.systemManager.sources.menus.MenuModel',
                    defaults: {
                        anchor: '100%',
                        labelWidth: 80,
                        labelSeparator: '：'
                    },
                    gridPanel: me.getView(),
                    formData: record,
                    items: me.formItems()
                }
            }).show();
        }
        if (command === 'init') {
            EUtil.showMsg({
                option: 3,
                title: '温馨提示',
                message: '您确定初始化<span style="color:red;">『' + record.data.text + '』</span>的相关权限吗？初始化后会删除原权限定义',
                callback: function (btn) {
                    if (btn === 'ok') {
                        EUtil.RS({
                            url: reqUrl.sys.right.initRight,
                            params: { ids: record.data.id },
                            callback: function (resp) {
                                if (resp.success) {
                                    EUtil.showSuccess('初始化权限成功');
                                    store.reload();
                                } else {
                                    EUtil.showError('初始化权限『' + record.data.text + '』失败！');
                                }
                            }
                        });
                    }
                }
            });
        }
        if (command === 'remove') {/** 删除*/
            EUtil.showMsg({
                option: 3,
                title: '温馨提示',
                message: '您确定删除<span style="color:red;">『' + record.data.text + '』</span>吗？',
                callback: function (btn) {
                    if (btn === 'ok') {
                        EUtil.RS({
                            url: reqUrl.sys.sources.menus.destroy,
                            params: { ids: record.data.id },
                            callback: function (resp) {
                                if (resp.success) {
                                    store.remove(record);
                                    store.reload();
                                } else {
                                    EUtil.showError(resp.msg || '删除资源『' + record.data.text + '』失败！');
                                }
                            }
                        });
                    }
                }
            });
        }
    }
});